package com.lcc.control;

import com.lcc.pojo.Taccount;
import com.lcc.service.AccountService;
import com.lcc.service.BookService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author 廖聪聪
 * @version 1.0
 * 创建时间 2020/7/27 14:09
 */
@Controller
@RequestMapping("/account")
public class AccountControl {
	@Autowired
	private AccountService accountService;
	@Autowired
	private BookService bookService;

	/**
	 * 查询对应用户的余额
	 */
	@RequestMapping(value = "findAccout")
	public void findAccout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		   Object userid = request.getSession().getAttribute("userid");
		   List<Taccount> byAccout = accountService.findByAccout(new Integer((Integer) userid));
		   System.out.println(byAccout);
		   //使用json
		   JSONArray jsonArray = JSONArray.fromObject(byAccout);
		   String json = jsonArray.toString();
		   System.out.println(json);
		   response.setContentType("text/html;charset=utf-8");
		   response.getWriter().write(json);
	}
	@RequestMapping(value = "updateStoban")
	public String updateStoban(Integer num,Integer accid,Integer bookid){
        bookService.buybook(num,bookid,accid);
		return "redirect:/book/findallbook";
	}
	/**
	 * 配置处理异常信息
	 */
	/*@ExceptionHandler
	public String handlerError(Exception e, Model model){
		if (e instanceof MoneyLessException){
			model.addAttribute("msg","钱不够了！！！");
		}
		return "error_moneyless";
	}*/
}
